package com.ruoyi.chiwen.util;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.*;
import com.tencentcloudapi.faceid.v20180301.FaceidClient;
import com.tencentcloudapi.faceid.v20180301.models.*;
/**
 * @program: ruoyi
 * @description: TencentUtil
 * @author: 佐佐木
 * @create: 2021-06-16 22:43
 **/
public class TencentCloudUtil {
    public static  final String SecretId1="AKIDwhCQO5XmkQqypPhjUzC8nCJAk46rMaXD";
    public static  final String SecretKey1="QaT5QcgV4OSFl0yf5E6h14qLYUAZhY8R";

    public static void main(String[] args) {
////        测试 身份证识别
        String imgUrl="https://ocr-demo-1254418846.cos.ap-guangzhou.myqcloud.com/card/IDCardOCR/IDCardOCR1.jpg";
        String s = IdCard(SecretId1,SecretKey1,imgUrl);
        System.out.println(s);
//        {"Name":"刘洋","Sex":"女","Nation":"汉","Birth":"1995/5/13","Address":"广东省深圳市南山区腾讯大厦","IdNum":"440305199505132561","Authority":"","ValidDate":"","AdvancedInfo":"{}","RequestId":"4f50f73e-877c-4f9b-abcf-5a7f33daff62"}


////        银行卡识别
//        String imgUrl="https://ocr-demo-1254418846.cos.ap-guangzhou.myqcloud.com/card/BankCardOCR/BankCardOCR1.jpg";
//        String s = bankCard(SecretId1,SecretKey1,imgUrl);
//        System.out.println(s);
//        {"CardNo":"6225768888888888","BankInfo":"招商银行(03080000)","ValidDate":"07/2023","CardType":"贷记卡","CardName":"招商银行信用卡","RequestId":"02101720-7c0b-413b-b99f-dac73d0af8c9"}


//        获取动作顺序
//        String v = getActiveOrd(SecretId1,SecretKey1);
//        System.out.println(v);

        //   获取数字验证码
//        String u = getActiveOrd(SecretId1,SecretKey1);
//        System.out.println(u);

//        活体人脸核身
//        String idNo = "411099190020242415";
//        String UserName = "李四";
//        String videoBase64 = "123";
//        String ordNo = "1,2";
    }

    //    活体人脸核身
    public static  String authFace(String SecretId,String SecretKey,String idNo,String UserName,String videoBase64 ,String ordNo){
        try{
            Credential cred = new Credential(SecretId, SecretKey);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("faceid.tencentcloudapi.com");
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            FaceidClient client = new FaceidClient(cred, "ap-beijing", clientProfile);
            LivenessRecognitionRequest req = new LivenessRecognitionRequest();
            req.setIdCard(idNo);
            req.setName(UserName);
            req.setVideoBase64(videoBase64);
            req.setLivenessType("ACTION");
            req.setValidateData(ordNo);
            LivenessRecognitionResponse resp = client.LivenessRecognition(req);

            System.out.println(LivenessRecognitionResponse.toJsonString(resp));
            return LivenessRecognitionResponse.toJsonString(resp);

            //        {
            //            "Response": {
            //            "Result": "Success",
            //                    "Description": "成功",
            //                    "BestFrameBase64": "<Imagebase64>",
            //                    "BestFrameList": [
            //            "xx"
            //    ],
            //            "Sim": 89.88,
            //                    "RequestId": "f904f4cf-75db-4f8f-a5ec-dc4f942c7f7a"
            //        }
            //        }
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
            return "false";
        }

    }

    //获取数字验证码
    public static  String getNumOrd(String SecretId,String SecretKey){
        try{
            Credential cred = new Credential(SecretId, SecretKey);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("faceid.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            FaceidClient client = new FaceidClient(cred, "ap-beijing", clientProfile);

            GetLiveCodeRequest req = new GetLiveCodeRequest();

            GetLiveCodeResponse resp = client.GetLiveCode(req);

            System.out.println(GetLiveCodeResponse.toJsonString(resp));
            return GetLiveCodeResponse.toJsonString(resp);
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
        return "false";
    }

    // 获取动作顺序
    public static  String getActiveOrd(String SecretId,String SecretKey){
        try{
            Credential cred = new Credential(SecretId, SecretKey);

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("faceid.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            FaceidClient client = new FaceidClient(cred, "ap-beijing", clientProfile);
            GetActionSequenceRequest req = new GetActionSequenceRequest();
            GetActionSequenceResponse resp = client.GetActionSequence(req);
//            System.out.println(GetActionSequenceResponse.toJsonString(resp));
            return GetActionSequenceResponse.toJsonString(resp);
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
        return "false";
    }

    // 识别银行卡号
    public static String bankCard(String SecretId,String SecretKey,String imgUrl){
        try{
            Credential cred = new Credential(SecretId, SecretKey);

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("ocr.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            OcrClient client = new OcrClient(cred, "ap-beijing", clientProfile);

            BankCardOCRRequest req = new BankCardOCRRequest();
            req.setImageUrl(imgUrl);
            BankCardOCRResponse resp = client.BankCardOCR(req);

            System.out.println(BankCardOCRResponse.toJsonString(resp));
            return BankCardOCRResponse.toJsonString(resp);
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
        return "false";
    }

    // 识别身份卡号
    public static String IdCard(String SecretId,String SecretKey,String ImgUrl){
        try{
            Credential cred = new Credential(SecretId, SecretKey);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("ocr.tencentcloudapi.com");
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            OcrClient client = new OcrClient(cred, "ap-beijing", clientProfile);
            IDCardOCRRequest req = new IDCardOCRRequest();
            req.setImageUrl(ImgUrl);
            IDCardOCRResponse resp = client.IDCardOCR(req);
            return IDCardOCRResponse.toJsonString(resp);
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
        return "false";
    }
}
